﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;


namespace Pongster
{
    public class StartMenuScreen : BaseGameState
    {
        PictureBox backgroundImage;
        PictureBox stopImageL;
        PictureBox stopImageR;
        LinkLabel startGame;
        LinkLabel loadGame;
        LinkLabel options;
        LinkLabel exitGame;
        float maxItemWidth = 0f;
        
        public StartMenuScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        public override void Initialize()
        {            
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager Content = Game.Content;
                        
            backgroundImage = new PictureBox(Content.Load<Texture2D>(@"Backgrounds\mainmenubg"), GameRef.BaseRectangle);
            ControlManager.Add(backgroundImage);

            Texture2D stopTexture = Content.Load<Texture2D>(@"GUI\stopbar");
            stopImageL = new PictureBox(stopTexture, new Rectangle(0, 0, stopTexture.Width, stopTexture.Height));
            ControlManager.Add(stopImageL);
            stopImageR = new PictureBox(stopTexture, new Rectangle(0, 0, stopTexture.Width, stopTexture.Height));
            ControlManager.Add(stopImageR);

            //Labels

            Label startLabel = new Label();

            startLabel.Text = "Main Menu";
            startLabel.Size = startLabel.SpriteFont.MeasureString(startLabel.Text);
            startLabel.Position = new Vector2((Settings.basescreenWidth / 2), 85);
            startLabel.Scale = 0.9f;
            startLabel.Origin = new Vector2(startLabel.Size.X / 2, 0);
            ControlManager.Add(startLabel);

            //Links

            startGame = new LinkLabel();
            startGame.Text = "New Game";
            startGame.Size = startGame.SpriteFont.MeasureString(startGame.Text);
            startGame.Selected += new EventHandler(menuItem_Selected);

            ControlManager.Add(startGame);

            loadGame = new LinkLabel();
            loadGame.Text = "Continue";
            loadGame.Size = loadGame.SpriteFont.MeasureString(loadGame.Text);
            loadGame.Selected += menuItem_Selected;

            ControlManager.Add(loadGame);

            options = new LinkLabel();
            options.Text = "Options";
            options.Size = loadGame.SpriteFont.MeasureString(options.Text);
            options.Selected += menuItem_Selected;

            ControlManager.Add(options);

            exitGame = new LinkLabel();
            exitGame.Text = "Exit";
            exitGame.Size = exitGame.SpriteFont.MeasureString(exitGame.Text);
            exitGame.Selected += menuItem_Selected;

            ControlManager.Add(exitGame);
            
            ControlManager.NextControl();

            ControlManager.FocusChanged += new EventHandler(ControlManager_FocusChanged);


            //Links Positions

            foreach (Control c in ControlManager)
            {
                if (c is LinkLabel)
                {
                    if (c.Size.X > maxItemWidth)
                        maxItemWidth = c.Size.X;
                }
            }

            Vector2 position = new Vector2(Settings.basescreenWidth / 2, Settings.basescreenHeight / 2 + 20);

            foreach (Control c in ControlManager)
            {
                if (c is LinkLabel)
                {
                    c.Origin = new Vector2(c.Size.X / 2, 0);

                    if (c.Size.X > maxItemWidth)
                        maxItemWidth = c.Size.X;

                    c.Position = position;
                    position.Y += c.Size.Y + 20f;
                    c.Color = Color.Ivory;
                }
            }

            ControlManager_FocusChanged(startGame, null);

        }


        void ControlManager_FocusChanged(object sender, EventArgs e)
        {
            Control control = sender as Control;
            Vector2 position = new Vector2(control.Position.X + control.Size.X/2 + 10, control.Position.Y + 2);
            stopImageR.SetPosition(position);
            position = new Vector2(control.Position.X - control.Size.X / 2 - 55, control.Position.Y + 2);
            stopImageL.SetPosition(position);
        }

        private void menuItem_Selected(object sender, EventArgs e)
        {
            GameRef.SoundManager.PlaySFX("select", 0.8f, 0, 0, null, null);
            if (sender == startGame)
            {
                //StateManager.ChangeState(GameRef.GamePlayScreen);
                Transition(ChangeType.Change, new GamePlayScreen(GameRef, GameRef.StateManager));
            }

            if (sender == loadGame)
            {
                //StateManager.PushState(GameRef.GamePlayScreen);
                Transition(ChangeType.Push, GameRef.GamePlayScreen);
            }

            if (sender == options)
            {
                //StateManager.PushState(GameRef.OptionsScreen);;
                Transition(ChangeType.Push, GameRef.OptionsScreen);
            }

            if (sender == exitGame)
            {
                GameRef.SoundManager.StopAll();
                GameRef.SoundManager.StopSong();
                GameRef.Exit();
            }

        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime);
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();
                        
            ControlManager.Draw(GameRef.SpriteBatch);

            base.Draw(gameTime);

            GameRef.SpriteBatch.End();
        }

    }
}
